const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { optimize, library } = require("webpack");
// const { ModuleFederationPlugin } = require("webpack").container;
const { ModuleFederationPlugin } = require('@module-federation/enhanced/webpack');
const packageName = require('./package.json').name;

module.exports = {
  mode: "development",
  entry: "./index.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    publicPath: "auto",
      library: `${packageName}`,
    libraryTarget: 'umd',
    chunkLoadingGlobal: `webpackJsonp_${packageName}`,
  },
  optimization: { splitChunks: false },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-react"],
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      title: "App1",
      filename: "index.html",
    }),
    new ModuleFederationPlugin({
      name: "app1",
      remotes: {
        app2: "app2@http://localhost:9999/app2.js",
      },
      filename: "app1.js",
      exposes: {
        "./App": "./App.js",
        "./Button": "./Button.jsx",
      },
      shared: { react: { singleton: true, eager: false }, 'react-dom': { singleton: true, eager: false } },
    }),
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, "public"),
    },
    compress: true,
    port: 8888,
    host: "localhost",
    headers: {
      'Access-Control-Allow-Origin': 'http://localhost:3000' // 允许主站访问资源
    }
  },
  resolve: {
    extensions: [".jsx", ".js"],
  },
  stats: "minimal",
};
